package com.hlhj.funlike_business.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.mymvp.mvp.BaseFgm;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.CashierAty;
import com.hlhj.funlike_business.activity.CommentAty;
import com.hlhj.funlike_business.activity.MyFoodTypeAty;
import com.hlhj.funlike_business.activity.PreferentialAty;
import com.hlhj.funlike_business.activity.SettingsAty;
import com.hlhj.funlike_business.activity.adapter.ActivityAdp;
import com.hlhj.funlike_business.bean.FoodDetailBean;
import com.hlhj.funlike_business.bean.ShopInfoBean;
import com.hlhj.funlike_business.data.SPUser;
import com.hlhj.funlike_business.presenter.MineFgmPresenter;
import com.hlhj.funlike_business.views.MineFgmView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseFgm<MineFgmView,MineFgmPresenter> implements MineFgmView {
    private TextView tv_business,tv_shopname,tv_advert;
    private ImageView img_mine_bg;
    private RecyclerView recycle_list;
    private ActivityAdp atyAdp;
    private int type;
    private List<ShopInfoBean.DataBean.ActivityBean> activityBeans;
    private RelativeLayout rl_setting,rl_preferential,rl_cashier,rl_comment,rl_foodtype,rl_loginout;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Toast.makeText(getContext(),"暂停营业成功",Toast.LENGTH_SHORT).show();
                    tv_business.setText("恢复营业");
                    tv_business.setBackground(getResources().getDrawable(R.drawable.yingye_selected));
                    type=2;
                    break;
                case 2:
                    tv_business.setText("暂停营业");
                    tv_business.setBackground(getResources().getDrawable(R.drawable.yingye_selected_normal));
                    Toast.makeText(getContext(),"恢复营业成功",Toast.LENGTH_SHORT).show();
                    type=1;
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void doBeforInit() {

    }

    @Override
    public int getContentId() {
        return R.layout.fgm_mine;
    }

    @NotNull
    @Override
    public MineFgmView bindView() {
        return this;
    }

    @NotNull
    @Override
    public MineFgmPresenter createPresenter() {
        return new MineFgmPresenter();
    }

    @Override
    public void initView() {
        tv_business=rootView.findViewById(R.id.tv_business);
        img_mine_bg=rootView.findViewById(R.id.img_mine_bg);
        rl_setting=rootView.findViewById(R.id.rl_setting);
        rl_preferential=rootView.findViewById(R.id.rl_preferential);
        rl_cashier=rootView.findViewById(R.id.rl_cashier);
        rl_comment=rootView.findViewById(R.id.rl_comment);
        rl_foodtype=rootView.findViewById(R.id.rl_foodtype);
        recycle_list=rootView.findViewById(R.id.recycle_list);
        tv_shopname=rootView.findViewById(R.id.tv_shopname);
        tv_advert=rootView.findViewById(R.id.tv_advert);
        rl_loginout=rootView.findViewById(R.id.rl_loginout);
    }

    @Override
    public void logicStart() {
        activityBeans=new ArrayList<>();
        recycle_list.setLayoutManager(new GridLayoutManager(getContext(),3));
        atyAdp = new ActivityAdp(activityBeans);
        recycle_list.setAdapter(atyAdp);

        type = SPUser.getUser(getActivity().getApplication()).getShopStatus();
        if (type==1){
            tv_business.setText("暂停营业");
            tv_business.setBackground(getResources().getDrawable(R.drawable.yingye_selected_normal));
        }else {
            tv_business.setText("恢复营业");
            tv_business.setBackground(getResources().getDrawable(R.drawable.yingye_selected));
        }
        getPresenter().LoadShopInfo(getActivity());
    }


    @Override
    public void setListener() {
        rl_preferential.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), PreferentialAty.class));
            }
        });
        rl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingsAty.class));
            }
        });
        rl_cashier.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), CashierAty.class));
            }
        });
        rl_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), CommentAty.class));
            }
        });
        rl_foodtype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),MyFoodTypeAty.class));
            }
        });
        tv_business.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1营业 2非营业
                getPresenter().LoadShopStatus(getContext(),type);
            }
        });
        rl_loginout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUser.getUser(getActivity().getApplication()).setToken("");
                System.exit(0);
            }
        });
    }

    @Subscribe
    public void refresh(String msg){
        if (msg.equals("刷新我的界面")){
            getPresenter().LoadShopInfo(getActivity());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showShopStatus(String msg) {
        if (type==1){
            handler.sendEmptyMessage(1);
        }else if (type==2){
            handler.sendEmptyMessage(2);
        }
    }

    @Override
    public void showShopInfo(ShopInfoBean.DataBean dataBean) {
        Glide.with(getActivity()).load(dataBean.getImg()).into(img_mine_bg);
        tv_shopname.setText(dataBean.getShop_name());
        tv_advert.setText(dataBean.getAdvertising());
        activityBeans.clear();
        activityBeans.addAll(dataBean.getActivity());
        atyAdp.notifyDataSetChanged();
    }
}
